<template>
    <div class="maintenanceWarning">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
            <el-row>
                <el-col :span="24" style="display: flex; justify-content: center; ">
                    <span style="font-size: 20px; color: #000; font-weight: bold;">保养预警</span>
                </el-col>
            </el-row>
            <!-- <p></p> -->
            <el-row :gutter="24">
                <el-col :span="4">
                    <el-form-item>
                        <el-input  v-model="dataForm.deviceName" placeholder="设备名称" clearable>
                            <el-button @click="search()" slot="append" icon="el-icon-search"></el-button>
                        </el-input>
                        <!-- <el-button v-if="isAuth('dss:warning:maintenance')" type="warning" @click="maintenanceHandle()" :disabled="dataListSelections.length <= 0">批量保养</el-button> -->
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item>
                 <el-button v-if="isAuth('dss:warning:maintenance')" type="warning" @click="maintenanceHandle()" :disabled="dataListSelections.length <= 0">批量保养</el-button>   
                <!-- <p></p> -->
                </el-form-item>
                </el-col>
                <!-- <el-col :span="4" :offset="2">
                    
                </el-col> -->
                <el-col :span="9" :offset="5">
                    <el-form-item label="保养计划：" prop="maintenancePlan"></el-form-item>
                    <el-select v-model="dataForm.maintenancePlan" placeholder="保养计划" clearable style="width: 150px;" @change="search()">
                        <el-option label="方案一" value="1"></el-option>
                        <el-option label="方案二" value="2"></el-option>
                </el-select>
                </el-col>
            </el-row>
        </el-form>
        <!-- 表字段 -->
         <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
            <el-table-column  type="selection" header-align="center" align="center" width="50"></el-table-column>
            <el-table-column type="index" header-align="center" align="center" label="序号"></el-table-column>
            <el-table-column prop="deviceId" header-align="center" align="center" label="设备编号"></el-table-column>

            <el-table-column prop="deviceName" header-align="center" align="center" label="设备名称"></el-table-column>
            <el-table-column prop="maintenancePlan" header-align="center" align="center" label="保养计划">
                <template slot-scope="scope">
                    <span v-if="scope.row.maintenancePlan === '1'">方案一</span>
                    <span v-if="scope.row.maintenancePlan === '2'">方案二</span>
                </template>
            </el-table-column>
            <el-table-column prop="maintenanceStatus" label="保养状态" header-align="center" align="center">
            <template slot-scope="scope">
                <el-popover trigger="hover" placement="top">
                    <p>车间名称: {{ scope.row.workshopName }}</p>
                    <p v-if="scope.row.deviceType == 1">设备类型: 生产设备</p>
                    <p v-if="scope.row.deviceType == 2">设备类型: 其他设备</p>
                    <p style="color: #E6A23C;">保养预警时间: {{ scope.row.noticeTime }}</p>
                    <p style="color: red;">保养到期时间: {{ scope.row.maintenanceDueTime }}</p>
                    <!-- <div slot="reference" class="name-wrapper"> -->
                    <div slot="reference">
                        <!-- <el-tag v-if="scope.row.maintenanceStatus === 1" size="medium" type="success">已保养</el-tag> -->
                        <el-tag v-if="scope.row.maintenanceStatus === 0" size="medium" type="warning">保养中</el-tag>
                        <el-tag v-if="scope.row.maintenanceStatus === -1" size="medium" type="danger">未保养</el-tag>
                    </div>
                </el-popover>
            </template>
        </el-table-column>
            <el-table-column  fixed="right"  header-align="center"  align="center"  width="150"  label="操作">
                <template slot-scope="scope">
                    <!-- <el-button v-if="isAuth('dss:warning:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button> -->
                    <el-button v-if="isAuth('dss:warning:update') && scope.row.maintenanceStatus != -1 &&  scope.row.maintenanceStatus != null" 
                    type="primary" size="small" @click="finishHandle(scope.row.id)" round>保养完成</el-button>
                    <el-button v-if="isAuth('dss:warning:maintenance') && scope.row.maintenanceStatus == -1" type="warning" size="small" @click="maintenanceHandle(scope.row.id)" round>开始保养</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- <el-switch v-model="value"></el-switch> -->
        <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :hide-on-single-page="value"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 新增/修改 弹窗 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
</template>

<script>
import AddOrUpdate from './maintenanceWarning-add-or-update.vue'
export default {
    data(){
        return{
            dataForm:{
                deviceId:'',
                deviceName:'',
                // planList:'',
                maintenancePlan:'',
            },
            pageIndex:1,
            pageSize:10,
            dataListLoading:false,
            dataList:[],
            dataListSelections:[],
            totalPage:0,
            addOrUpdateVisible:false,
            value:false
        }
    },
    components:{
        AddOrUpdate
    },
    activated() {
        this.getDataList()
    },
    methods:{
        handleEdit(index, row) {
        console.log(index, row);
      },
      handleDelete(index, row) {
        console.log(index, row);
      },
        //查询按钮
        search(){
            this.pageIndex = 1
            this.getDataList()
        },
        //获取数据
        getDataList(){
            this.$http({
                url:this.$http.adornUrl('/dss/maintenanceWarning/list'),
                method:'get',
                params: this.$http.adornParams({
                    'page': this.pageIndex,
                    'limit': this.pageSize,
                    'deviceName': this.dataForm.deviceName,

                    'maintenancePlan':this.dataForm.maintenancePlan,
                })
            }).then(({data})=>{
                if(data && data.code === 0){
                    console.log(data)
                    this.dataList = data.page.list
                    // console.log(this.dataList)
                    this.totalPage = data.page.totalCount
                
                    // this.dataForm.maintenancePlan = data.maintenancePlan

                }else{
                    this.dataList = []
                    this.totalPage = 0
                }
                this.dataListLoading = false
                // console.log(this.dataForm.planList);
            })
        },
        //多选
        selectionChangeHandle(val){
            this.dataListSelections = val
        },
        //保养
        maintenanceHandle(id){
            // console.log('1111111111',this.dataListSelections)
            var ids = id ? [id] : this.dataListSelections.map(item => {
                // 获取多选集合中的deviceId字段
                return item.id
            })
            this.$confirm(`确定对[设备编号=${ids.join(',')}]进行[${id ? '保养' : '批量保养'}]操作?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http({
                    url:this.$http.adornUrl('/dss/maintenanceWarning/maintenance'),
                    method: 'post',
                    data: this.$http.adornData(ids, false)
                }).then(({data}) => {
                    if(data && data.code === 0){
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.getDataList()
                            }
                        })
                    }else{
                        this.$message.error(data.msg)
                    }
                })
            })
        },
        //保养完成
        finishHandle(id){
            var ids = id ? [id] : this.dataListSelections.map(item => {
                return item.id
            })
            this.$confirm(`确定对[设备编号=${ids.join(',')}]进行[${id ? '保养完成' : '批量保养完成'}]操作?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http({
                    url:this.$http.adornUrl('/dss/maintenanceWarning/finish'),
                    method: 'post',
                    data: this.$http.adornData(ids, false)
                }).then(({data}) => {
                    if(data && data.code === 0){
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.getDataList()
                            }
                        })
                    }else{
                        this.$message.error(data.msg)
                    }
                })
            })
        },
        //每页数
        sizeChangeHandle(val){
            this.pageSize = val
            this.pageIndex = 1
            this.getDataList()
        },
        //当前页
        currentChangeHandle(val){
            this.pageIndex = val
            this.getDataList()
        },
        //新增
        addOrUpdateHandle (id){
            // console.log(id)
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
            })
        }
    }
}
</script>
